package com.agricultural.robot.model;

import com.agricultural.robot.observer.Observer;
import com.agricultural.robot.state.MachineState;
import com.agricultural.robot.strategy.PathStrategy;

/**
 * 农机基类 - 实现观察者接口
 */
public abstract class AgriculturalMachine implements Observer {
    private String name;
    private double batteryLevel;
    private double positionX;
    private double positionY;
    private MachineState state;
    private PathStrategy pathStrategy;

    public AgriculturalMachine(String name) {
        this.name = name;
        this.batteryLevel = 100.0;
        this.positionX = 0.0;
        this.positionY = 0.0;
    }

    public void start() {
        state.start(this);
    }

    public void stop() {
        state.stop(this);
    }

    public void pause() {
        state.pause(this);
    }

    public void resume() {
        state.resume(this);
    }

    public void setPathStrategy(PathStrategy strategy) {
        this.pathStrategy = strategy;
    }

    public Path planPath(Field field, String startPoint, String endPoint) {
        return pathStrategy.planPath(field, startPoint, endPoint);
    }

    public abstract void performTask(Task task);

    public void setState(MachineState state) {
        this.state = state;
    }

    public String getName() {
        return name;
    }

    public double getBatteryLevel() {
        return batteryLevel;
    }

    public void setBatteryLevel(double batteryLevel) {
        this.batteryLevel = batteryLevel;
    }

    public double getPositionX() {
        return positionX;
    }

    public void setPositionX(double positionX) {
        this.positionX = positionX;
    }

    public double getPositionY() {
        return positionY;
    }

    public void setPositionY(double positionY) {
        this.positionY = positionY;
    }

    public MachineState getState() {
        return state;
    }

    public PathStrategy getPathStrategy() {
        return pathStrategy;
    }

    public void updatePosition(double x, double y) {
        this.positionX = x;
        this.positionY = y;
        System.out.println(name + " 位置更新: (" + x + ", " + y + ")");
    }

    public void consumeBattery(double amount) {
        this.batteryLevel -= amount;
        if (this.batteryLevel < 0) {
            this.batteryLevel = 0;
        }
        System.out.println(name + " 电量更新: " + this.batteryLevel + "%");
    }

    @Override
    public void update(EnvironmentData data) {
        System.out.println(name + " 收到环境数据更新: " + data);
        // 根据环境数据调整农机行为
        if (data.getSoilMoisture() > 70) {
            System.out.println(name + " 检测到土壤湿度过高，调整作业策略");
            // 这里可以实现根据湿度调整播种深度等逻辑
        }
        if (data.getTemperature() > 35) {
            System.out.println(name + " 检测到温度过高，调整作业速度");
            // 这里可以实现根据温度调整作业速度等逻辑
        }
    }
}
